﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：商品管理Ajax请求服务端处理程序。
  //创建人：赵国伟
  //生成时间：2012/03/18 12:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Shopping.Common;
using Shopping.Model;
using Shopping.BLL;

public partial class Manage_AS_GoodsCommon : BaseManagePage
{
    private readonly CategoriesManage bllCategory = new CategoriesManage();
    private readonly BrandsManage bllBrand = new BrandsManage();
    private readonly GoodsManage bllGoods = new GoodsManage();
    private readonly GoodsPlacesManage bllPlace = new GoodsPlacesManage();
    private readonly ConsultManage bllConsult = new ConsultManage();
    private readonly EvaluateManage bllEvaluate = new EvaluateManage();
    private readonly CommendManage bllCommend = new CommendManage();
  

    private ReturnValue ret = new ReturnValue(ResultType.Fail); 

    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["Action"] as string;
        switch (action)
        {
            case "SaveCategory":   //保存类别信息； by 赵国伟 2012-03-18
                SaveCategory();
                break;
            case "GetCategoryInfo":  //获取一条商品类别信息；by 赵国伟 2012-03-18
                GetCategoryInfo();
                break;
            case "DeleteCategory":  //删除一条商品类别信息； by 赵国伟 2012-03-18
                DeleteCategory();
                break;
            case "ModifyCategoryStatus": //调整类别的状态：by 赵国伟 2012-03-28
                ModifyCategoryStatus();
                break;
            case "SaveBrand":  //保存一条品牌信息； by 赵国伟 2012-04-02
                SaveBrand();
                break;
            case "GetBrand": //查询一条指定的品牌信息；by 赵国伟 2012-04-02
                GetBrand();
                break;
            case "DeleteBrand": //删除一条品牌信息； by 赵国伟 2012-04-02;
                DeleteBrand();
                break;
            case "SaveGoodsPlace":  //保存一条产地信息； by 赵国伟 2012-04-04
                SaveGoodsPlace();
                break;
            case "GetGoodsPlaceInfo": //查询一条指定的产地信息；by 赵国伟 2012-04-04
                GetGoodsPlaceInfo();
                break;
            case "DeleteGoodsPlace": //删除一条产地信息； by 赵国伟 2012-04-04;
                DeleteGoodsPlace();
                break;
            case "GetBrandsByCategory": //查询指定类别下的品牌信息； by 赵国伟 2012-04-08
                GetBrandsByCategory();
                break;
            case "SaveGoods": //保存商品信息；by 赵国伟  2012-04-09
                SaveGoods();
                break;
            case "GetGoods": //查询一条商品信息；by 赵国伟  2012-04-10
                GetGoods();
                break;
            case "DeleteGoods":  //删除指定的一条商品信息；by 赵国伟 2012-05-29
                DeleteGoods();
                break;
            case "QueryGoodsAsPage"://分页查询商品信息；by 赵国伟 2012-04-21
                QueryGoodsAsPage();
                break;
            case "ReplyConsult"://咨询回复; by 赵国伟 2012-04-14
                ReplyConsult();
                break;
            case "DeleteConsult": //删除一条咨询信息； by 赵国伟 2012-04-14；
                DeleteConsult();
                break;
            case "DeleteEvaluate": //删除一条评价信息； by 赵国伟 2012-04-14；
                DeleteEvaluate();
                break;
            case "GetGoodsCommend": //查询一条商品推荐信息；by 赵国伟 2012-04-21
                GetGoodsCommend();
                break;
            case "DeleteGoodsCommend"://删除一条商品推荐信息；by 赵国伟 2012-04-21
                DeleteGoodsCommend();
                break;
            case "SaveGoodsCommend": //保存一条商品推荐信息；by 赵国伟 2012-04-21
                SaveGoodsCommend();
                break;

        }
        Response.End();
    }

    #region "保存类别信息 by 赵国伟 2012-03-18"
    /// <summary>
    /// 保存类别信息
    /// </summary>
    private void SaveCategory()
    {
        ret = BLLHelper.Save<CategoriesManage, Categories>("Save");
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "查询一条商品的类别信息 by 赵国伟 2012-03-18"
    /// <summary>
    /// 查询一条商品的类别信息
    /// </summary>
    private void GetCategoryInfo()
    {
        int categoryId = Utility.Cast2int(Request.QueryString["categoryId"]);
        Categories data = bllCategory.GetCategoryInfo(categoryId);
        if (data != null)
            Response.Write(data.ToJsonString());
    }
    #endregion

    #region "删除指定的类别信息 by 赵国伟 2012-03-18"
    /// <summary>
    /// 删除指定的类别信息
    /// </summary>
    private void DeleteCategory()
    {
        int categoryId = Utility.Cast2int(Utility.G("CategoryID"));
        ret = bllCategory.Delete(categoryId);
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "调整类别的状态； by 赵国伟 2012-03-28"
    /// <summary>
    /// 调整类别的状态；
    /// </summary>
    private void ModifyCategoryStatus()
    {
        int categoryId = Utility.Cast2int(Utility.G("CategoryID"));
        string status = Utility.G("Status");
        Status s;
        if (status == Status.正用.ToString())
            s = Status.正用;
        else if (status == Status.停用.ToString())
            s = Status.停用;
        else
        {
            ret.Message = "状态参数错误！";
            Response.Write(ret.ToJsonString());
            return;
        }

        if (bllCategory.UpdateStatus(categoryId, s) > 0)
        {
            ret.Message = "操作成功！";
            ret.Result = ResultType.Success;
        }
        else
            ret.Message = "操作失败，未找到对应的类别信息！";
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "保存一条品牌信息； by 赵国伟 2012-04-02"
    /// <summary>
    /// 保存一条品牌信息；
    /// </summary>
    private void SaveBrand()
    {
        //获取客户端提交上来的数据；结构为json结构；
        string jsonData = Request["jsonData"] + "";  //获取传递过来的Json对象数据；
        string BrandDescription = Utility.P("BrandDescription");
        BrandInfo info = null;
        ReturnValue ret = new ReturnValue();
        try
        {
            ///将提交上来的Json格式的数据装化为相应的实体类；
            info = JSONHelper.JsonStringToObject<BrandInfo>(jsonData);
        }
        catch (Exception e)
        {
            ret.Message = "数据提取失败！" + e.Message;
            Response.Write(ret.ToJsonString());
            return;
        }
        info.Brand.BrandDescription = BrandDescription;
        if (info != null)
            ret = bllBrand.Save(info);
        else
            ret.Message = "提交数据不能为空！";
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "查询一条指定的品牌信息； by 赵国伟  2012-04-02"
    /// <summary>
    /// 查询一条指定的品牌信息；
    /// </summary>
    private void GetBrand()
    {
        int brandId = Utility.G<int>("BrandID");
        BrandInfo b = bllBrand.GetBrand(brandId);
        if (b != null)
            Response.Write(JSONHelper.ObjectToJson<BrandInfo>(b));
    }
    #endregion

    #region "删除一条指定的品牌信息； by 赵国伟 2012-04-02"
    /// <summary>
    /// 删除一条指定的品牌信息；
    /// </summary>
    private void DeleteBrand()
    {
        int brandId = Utility.G<int>("BrandID");
        Response.Write(bllBrand.Delete(brandId).ToJsonString());
    }
    #endregion


    #region "保存一条产地信息； by 赵国伟 2012-04-04"
    /// <summary>
    /// 保存一条产地信息；
    /// </summary>
    private void SaveGoodsPlace()
    {
        ret = BLLHelper.Save<GoodsPlacesManage, GoodsPlaces>("Save");
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "查询一条指定的产地信息； by 赵国伟 2012-04-04"
    /// <summary>
    /// 查询一条指定的产地信息；
    /// </summary>
    private void GetGoodsPlaceInfo()
    {
        int GoodsPlaceID = Utility.G<int>("GoodsPlaceID");
        GoodsPlaces d = bllPlace.GetPlaceInfo(GoodsPlaceID);
        if (d != null)
            Response.Write(d.ToJsonString());
    }
    #endregion

    #region "删除一条指定的产地信息； by 赵国伟 2012-04-04"
    /// <summary>
    /// 删除一条指定的产地信息；
    /// </summary>
    private void DeleteGoodsPlace()
    {
        int GoodsPlaceID = Utility.G<int>("GoodsPlaceID");
        Response.Write(bllPlace.Delete(GoodsPlaceID).ToJsonString());
    }
    #endregion

    #region "查询指定类别下品牌信息； by 赵国伟 2012-04-08"
    /// <summary>
    /// 查询指定类别下品牌信息；
    /// </summary>
    private void GetBrandsByCategory()
    {
        int categoryId = Utility.G<int>("CategoryID");
        List<IDNameObj> listOfData = bllBrand.QueryBrands(categoryId, true);
        if (listOfData != null)
            Response.Write(JSONHelper.ObjectToJson<List<IDNameObj>>(listOfData));
    }
    #endregion

    #region "保存商品信息 by 赵国伟 2012-04-09"
    /// <summary>
    /// 保存商品信息
    /// </summary>
    private void SaveGoods()
    {
        //获取客户端提交上来的数据；结构为json结构；
        string jsonData = Request["jsonData"] + "";  //获取传递过来的Json对象数据；
        string Description = Utility.P("Description");
        GoodsInfo info = null;
        ReturnValue ret = new ReturnValue();
        try
        {
            ///将提交上来的Json格式的数据装化为相应的实体类；
            info = JSONHelper.JsonStringToObject<GoodsInfo>(jsonData);
        }
        catch (Exception e)
        {
            ret.Message = "数据提取失败！" + e.Message;
            Response.Write(ret.ToJsonString());
            return;
        }
        info.BaseInfo.Description = Description;
        if (info != null)
            ret = bllGoods.SaveGoods(info);
        else
            ret.Message = "提交数据不能为空！";
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "查询一条商品信息 by 赵国伟 2012-04-10"
    /// <summary>
    /// 查询一条商品信息
    /// </summary>
    private void GetGoods()
    {
        int GoodsID = Utility.G<int>("GoodsID");
        GoodsInfo g = bllGoods.GetGoods(GoodsID);
        if (g != null)
            Response.Write(JSONHelper.ObjectToJson<GoodsInfo>(g));
    }
    #endregion

    #region "删除一条指定的商品信息； by赵国伟 2012-05-29"
    /// <summary>
    /// 删除一条指定的商品信息；
    /// </summary>
    private void DeleteGoods()
    {
        int GoodsID = Utility.G<int>("GoodsID");
        Response.Write(bllGoods.DeleteGoods(GoodsID).ToJsonString());
    }
    #endregion


    #region "分页查询商品信息； by 赵国伟 2012-04-21"
    /// <summary>
    /// 分页查询商品信息；
    /// </summary>
    private void QueryGoodsAsPage()
    {
        QueryGoodsAsPage q = new Shopping.Model.QueryGoodsAsPage(false);
        q.KeyWords = Utility.G("KeyWords");
        q.CurPage = Utility.G<int>("CurPage");
        if (q.CurPage <= 0) q.CurPage = 1;

        List<GoodsInfo> listOfSource = bllGoods.QueryListAsPage(q);
        string result ="";
        if(listOfSource == null)
            result = "{\"TotalCount\":" + q.TotalCount + ",\"Data\":null}";
        else
            result = "{\"TotalCount\":" + q.TotalCount + ",\"Data\":" + JSONHelper.ObjectToJson<List<GoodsInfo>>(listOfSource) + "}";
        Response.Write(result);
        
        /*
        List<IDNameObj> listOfTarget = null;
        if (listOfSource != null)
        {
            listOfTarget = new List<IDNameObj>();
            for (int i = 0; i < listOfSource.Count; ++i)
            {
                IDNameObj d = new IDNameObj();
                d.ID = listOfSource[i].BaseInfo.GoodsID;
                d.Name = listOfSource[i].BaseInfo.GoodsName;
                listOfTarget.Add(d);
            }
        }
        if (listOfTarget != null)
        {
            string result = "{\"TotalCount\":" + q.TotalCount + ",\"Data\":" + JSONHelper.ObjectToJson<List<IDNameObj>>(listOfTarget) + "}";
            Response.Write(result);
        }
         * */
    }
    #endregion


    #region "咨询回复 by 赵国伟 2012-04-14"
    /// <summary>
    /// 咨询回复
    /// </summary>
    private void ReplyConsult()
    {
        int GoodsConsultID = Utility.P<int>("GoodsConsultID");
        string content = Utility.P("ReplyContent");
        if (bllConsult.Reply(GoodsConsultID, content) > 0)
        {
            ret.Result = ResultType.Success;
            ret.Message = "回复成功！";
        }
        else
            ret.Message = "回复失败！";
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "删除一条咨询信息  by 赵国伟 2012-04-14"
    /// <summary>
    /// 删除一条咨询信息
    /// </summary>
    private void DeleteConsult()
    {
        int GoodsConsultID = Utility.G<int>("GoodsConsultID");
        if (bllConsult.Delete(GoodsConsultID) > 0)
        {
            ret.Result = ResultType.Success;
            ret.Message = "删除成功！";
        }
        else
            ret.Message = "删除失败！";
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "删除一条评价信息  by 赵国伟 2012-04-14"
    /// <summary>
    /// 删除一条评价信息
    /// </summary>
    private void DeleteEvaluate()
    {
        int GoodsEvaluateID = Utility.G<int>("GoodsEvaluateID");
        if (bllEvaluate.Delete(GoodsEvaluateID) > 0)
        {
            ret.Result = ResultType.Success;
            ret.Message = "删除成功！";
        }
        else
            ret.Message = "删除失败！";
        Response.Write(ret.ToJsonString());
    }
    #endregion



    #region "查询一条商品推荐信息； by 赵国伟 2012-04-21"
    /// <summary>
    /// 查询一条商品推荐信息；
    /// </summary>
    private void GetGoodsCommend()
    {
        int GoodsCommendID = Utility.G<int>("GoodsCommendID");
        GoodsCommendInfo d = bllCommend.GetCommend(GoodsCommendID);
        if (d != null)
            Response.Write(JSONHelper.ObjectToJson<GoodsCommendInfo>(d));
    }
    #endregion

    #region "删除一条商品推荐信息；by 赵国伟 2012-04-21"
    /// <summary>
    /// 删除一条商品推荐信息；
    /// </summary>
    private void DeleteGoodsCommend()
    {
        int GoodsCommendID = Utility.G<int>("GoodsCommendID");
        Response.Write(bllCommend.Delete(GoodsCommendID).ToJsonString());
    }
    #endregion

    #region "保存一条商品推荐信息； by 赵国伟 2012-04-21"
    /// <summary>
    /// 保存一条商品推荐信息；
    /// </summary>
    private void SaveGoodsCommend()
    {
        ret = BLLHelper.Save<CommendManage, GoodsCommend>("Save");
        Response.Write(ret.ToJsonString());
    }
    #endregion








}